UIGiftDay = Class("UIGiftDay", UIComponentBase)

function UIGiftDay:OnCreate(id, parent)
    self.activeId = id
    self.parent = parent
    self.drawWrapper = GoWrapper.New()
    self.lihui = self:GetChild("lihui")
    self.lihui2 = self:GetChild("lihui2")
    self.banner = self:GetChild("bannerBg")
    --self.bannerChar = self:GetChild("bannerChar")
    self.bannerText = self:GetChild("bannerText")
    self.title = self:GetChild("title")
    self.bannerTime = self:GetChild("bannerTime")
    self.buyTips = self:GetChild("buyTips")
    self.btnBuy = self:GetChild("buttomBug")
    self.statusController = self:GetController("status")
    self.tipTypeController = self:GetController("tipType")
    self.giftList = self:SetDynamicList(self:GetChild("list"),self.updateItem, nil, true)
end

function UIGiftDay:Refresh()
    logicMgr.CostManager.updateActiveOpenStatus(self.activeId);
    self.btnBuy.onClick:Clear();
    self.severData, self.itemData, self.modelTableData = logicMgr.CostManager.getCostData(self.activeId);
    logicMgr.CostManager.updateCostHelp(self, self:GetChild("help"), self.modelTableData);
    self.giftList.numItems = #self.itemData + 1;
    HelperFunc.ListTranstionPlay(self.giftList);

    self.tipTypeController.selectedIndex = 1;
    self.bannerText.text = getLanguage("DailyConsumptionBannerTips10202")
    self.buyTips.text = getLanguage("Storetips1021001");
    --self.bannerChar.url = tostring(self.modelTableData.f_BannerHead);
    self.banner.url = tostring(self.modelTableData.f_Banner);
    self.title.text = getLanguage(self.modelTableData.f_Title, nil, "");
    self.parent:updateCostShowByTableOrJson(self.modelTableData.f_ResourceShow);
    logicMgr.CostManager.updateLiHui(self, self.drawWrapper, self.lihui, self.lihui2, self.modelTableData.f_HeroSpine)

    self.statusController.selectedIndex = 0;
    if self.severData.hasBuy then
        self.statusController.selectedIndex = 1;
        if self.severData.hasEffect then
            self.countDownNum = self.severData.giftEndTime - HelperFunc.GetServerTime();
            if self.countDownNum <= 0 then
                self.bannerTime.text = getLanguage("Sys_ActivityEnd");
            else
                self:TimeCall(self.OnCountDown, 1000, -1);
                self.bannerTime.text = string.format(getLanguage(self.modelTableData.f_BannerTips, nil, "%s"), HelperFunc.GetSurpTimeStr(self.countDownNum));
            end
        else
            local delayTime = self.severData.giftEndTime - self.severData.giftStartTime + 2;
            local surpDay = math.floor(delayTime / 24 / 3600)
            local day = string.format(getLanguage("Sys_Time_Day"), surpDay);
            --self.bannerTime.text = string.format(getLanguage(self.modelTableData.f_BannerTips, nil, "%s"), day);
            self.bannerTime.text = string.format(getLanguage("Storetips1021003"), day);
        end
    end

    if self.severData.tableItem ~= nil then
        local tableItem = self.severData.tableItem;
        self.btnBuy.text =  string.format(getLanguage("Storetips1021004"), tableItem.f_Price);
        self.btnBuy.onClick:Add(function()
            local pop = CtrlManager.GetCtrl(PanelNames.UICommonPop);
            pop.Init("58", function(b)
                if b then
                    --self:ClickBuy(tableItem.f_ItemId, tableItem.f_ProductId, 1, tableItem.f_Price);
                    logicMgr.CostManager.getRewardByIdAndIndex(self.activeId, tableItem.f_ItemId, tableItem.f_ProductId, 1, tableItem.f_Price, self.onActiveSuccess, self);
                end
            end, tableItem.f_Price);
        end);
    end
end

function UIGiftDay:onActiveSuccess()
    if self.severData ~= nil and self.severData.giftEndTime ~= nil then
        local delayTime = self.severData.giftEndTime - HelperFunc.GetServerTime();
        local surpDay = math.floor(delayTime / 24 / 3600)
        if surpDay >= 7 then
            UISysTips.AddMsg(getLanguage("storedesc10210"));
        else
            UISysTips.AddMsg(string.format(getLanguage("storedesc1021001"), HelperFunc.GetSurpTimeStr(delayTime)));
        end
    else
        UISysTips.AddMsg(getLanguage("storedesc10210"));
    end
end

function UIGiftDay:updateItem(index, itemObj)
    local tableData = self.itemData[index];
    if tableData == nil then
        itemObj.visible = false;
        return ;
    end
    local bg = itemObj:GetChild("bg");
    local mask = itemObj:GetChild("mask");
    local buy = itemObj:GetChild("numMoney");
    local ntext = itemObj:GetChild("ntext");
    local buyNum = itemObj:GetChild("numLimit");
    local fristItem = itemObj:GetChild("item");
    local itemList = itemObj:GetChild("itemList");
    local btnGet = itemObj:GetChild("get");
    local stateController = itemObj:GetController("state");
    local limitController = itemObj:GetController("limit");
    local isfreeController = itemObj:GetController("isfree");
    local bgmask = itemObj:GetController("bgmask");
    local original = itemObj:GetChild("original");
    local originalMoney = itemObj:GetChild("originalMoney");
    local discountController = itemObj:GetController("discount");
    if index > #self.itemData then
        bgmask.selectedIndex = 1;
    else
        bgmask.selectedIndex = 0;
    end
    if tableData.f_OriginPrice == nil or tableData.f_OriginPrice == "" or tonumber(tableData.f_OriginPrice) == 0 then
        discountController.selectedIndex = 0;
    else
        discountController.selectedIndex = 1;
        originalMoney.text = "￥"..tableData.f_OriginPrice;
        original.text = getLanguage("storedesc1021002");
    end
    local buyCount = self.severData.hasBuyMap[tostring(tableData.f_ItemId)] or 0;
    local limitCount = tonumber(tableData.f_Count);
    bg.url = tostring(tableData.f_BgPic);
    mask.url = tostring(tableData.f_BgPic);
    bg.onClick:Clear();
    btnGet.onClick:Clear();
    ntext.text = getLanguage("SellLimit");
    if tableData.f_Price == 0 then
        isfreeController.selectedIndex = 1;
        buy.text = getLanguage("FreeMoney");
    else
        isfreeController.selectedIndex = 0;
        buy.text = "￥"..tableData.f_Price;
    end
    buyNum.text = (limitCount - buyCount).."/"..limitCount;
    local reward = decodeJsonStrToTabel(tableData.f_MajorCommodityReward)[1];
    logicMgr.CostManager.RefreshItem(fristItem, reward);
    local listData = decodeJsonStrToTabel(tableData.f_CommonCommodityReward);
    logicMgr.CostManager.RefreshItemList(itemList, listData);

    if buyCount >= limitCount then
        stateController.selectedIndex = 1;
    else
        if tableData.f_Price == 0 then
            stateController.selectedIndex = 0;
            bg.onClick:Add(function() self:ClickBuy(tableData.f_ItemId, tableData.f_ProductId, 1, tableData.f_Price); end);
        else
            if self.severData.hasEffect then
                stateController.selectedIndex = 2;
                btnGet.onClick:Add(function()
                    local data = {};
                    table.insert(data, tableData.f_ItemId);
                    logicMgr.CostManager.getRewardById(self.activeId, data, nil, self);
                end);
            else
                stateController.selectedIndex = 0;
                bg.onClick:Add(function() self:ClickBuy(tableData.f_ItemId, tableData.f_ProductId, 1, tableData.f_Price); end);
            end
        end
    end
end

function UIGiftDay:ClickBuy(id, payId, num, money)
    logicMgr.CostManager.getRewardByIdAndIndex(self.activeId, id, payId, num, money, self.onBuySuccess, self);
end

function UIGiftDay:onBuySuccess()
end

function UIGiftDay:OnCountDown()
    if self.modelTableData.f_BannerTipsType ~= 1 then
        return true;
    end
    self.countDownNum = self.countDownNum - 1;
    if self.countDownNum <= 0 then
        self:RemoveTimeCall(self.OnCountDown)
        self.statusController.selectedIndex = 0;
        self.bannerTime.text = getLanguage("Sys_ActivityEnd");
        costHandler.reqGetOneMarketInfo(self.activeId);
        return true;
    end
    self.bannerTime.text = string.format(getLanguage(self.modelTableData.f_BannerTips, nil, "%s"), HelperFunc.GetSurpTimeStr(self.countDownNum));
end

function UIGiftDay:OnDestroy()
    if self.RoleLihui then
        destroyImmediate(self.RoleLihui)
        self.RoleLihui = nil
    end
    if self._package and UIPackage.GetById(self._package) then
        UIPackage.RemovePackage(self._package)
    end
    self:RemoveTimeCall(self.OnCountDown)
end